home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8174
/
8174.xpi
/
chrome
/
antbar.jar
/
content
/
prefs
/
prefs.js
next >
Wrap
Text File
|
2009-12-30
|
14KB
|
484 lines
//
// prefs.js
// firefox
//
// Created by Zak on 2008-06-17.
// Contributor Brian King (http://briks.si)
// Copyright 2008-2009 Ant.com. All rights reserved.
//
var AntPrefs =
{
/**
* Preferences, first run and update addons
*/
init: function ()
{
var self = AntPrefs;
var em = AntLib.CCSV("@mozilla.org/extensions/manager;1", "nsIExtensionManager");
//obtain toolbar version form the rdf
var version = em.getItemForID("anttoolbar@ant.com").version;
var displayMode = self.getAntBranch().getCharPref('mode');
if (displayMode == '')
self.setDisplayMode('statusbar');
if (version != self.version && self.version != '')
{
self.updated();
}
else if (self.getAntBranch().getBoolPref("firstrun"))
{
self.firstRun();
}
AntRankService.addButtonId(AntBar.rankLabels['statusbar-button'], AntBar.rankLabels['statusbar-separator']);
AntRankService.addButtonId(AntBar.rankLabels['toolbar-button']);
self.setDisplayMode(self.getDisplayMode());
self.version = version;
self.manageFlvDir();
self.manageDisplayMode();
self.setAntUserAgent();
self.prefWatcher.startup();
self.flashTrusting();
},
/**
* Manage the toolbar or the statusbar mode
*/
manageDisplayMode: function ()
{
var self = AntPrefs;
var enabled = {};
var labels = {
'toolbar' : 'antToolbar',
'statusbar': 'ant-video-statusbarpanel',
}
if (self.getDisplayMode() == 'statusbar')
{
enabled[labels['toolbar']] = false;
enabled[labels['statusbar']] = true;
}
else
{
enabled[labels['toolbar']] = true;
enabled[labels['statusbar']] = false;
}
for (var k in enabled)
{
AntLib.ob(k).setAttribute('hidden', !enabled[k]);
}
AntRankService.updateVisible();
},
/**
* firstRun: is call the absolute first time the toolbar is displayed
*/
firstRun: function ()
{
var self = AntPrefs;
// Something to consider, asking the user if they want to set the ant search engine as default
setTimeout(function ()
{
AntLib.openURL("http://www.ant.com/video-downloader/installed/", true);
}, 1000);
self.getAntBranch().setBoolPref("firstrun", false);
},
/**
* updated: is called after the toolbar has been updated
*/
updated: function ()
{
var self = AntPrefs;
setTimeout( function ()
{
AntLib.openURL("http://www.ant.com/video-downloader/updated/", true);
}, 1000);
try {
self.removeJunkAgent();
}
catch (e) {
AntLib.toLog("Error in removeJunkAgent : " + e);
}
},
/**
* etanageFlvDir: create it if doesn't exist, update the preference...
*/
manageFlvDir: function ()
{
var self = AntPrefs;
var flvDir;
var appDir;
if (!self.flvDir)
{
flvDir = AntLib.CCSV("@mozilla.org/file/directory_service;1", "nsIProperties").get("ProfD", AntLib.CI("nsIFile"));
appDir = AntLib.CCSV("@mozilla.org/file/directory_service;1", "nsIProperties").get("ProfD", AntLib.CI("nsIFile"));
flvDir.append("antbar");
self.flvDir = flvDir.path;
}
else
{
flvDir = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
flvDir.initWithPath(self.flvDir);
}
if (!flvDir.exists())
flvDir.create(AntLib.CI("nsIFile").DIRECTORY_TYPE, 0777);
},
/**
* Display a FilePicker to chose the Video directory
*/
selectFlvDir: function ()
{
var self = AntPrefs;
var nsIFilePicker = AntLib.CI("nsIFilePicker");
var fp = AntLib.CCIN("@mozilla.org/filepicker;1", "nsIFilePicker");
var path = AntLib.ob("AntPrefsFlvDir").value;
if (path)
{
var file = AntLib.CCIN("@mozilla.org/file/local;1", "nsILocalFile");
file.initWithPath(path);
fp.displayDirectory = file;
}
fp.init(window, AntLang.getString("AntPrefs.selectFlvDirTxt"), AntLib.CI("nsIFilePicker").modeGetFolder);
var res = fp.show();
if (res == nsIFilePicker.returnOK)
{
path = fp.file.path;
AntLib.ob("AntPrefsFlvDir").value = path;
self.flvDir = path;
}
},
/**
* prefWatcher : observe Preferences and change UI if needed
*/
prefWatcher:
{
prefs: null,
startup: function()
{
var self = AntPrefs.prefWatcher;
self.prefs = AntPrefs.getAntBranch();
self.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
self.prefs.addObserver("", self, false);
},
shutdown: function()
{
var self = AntPrefs.prefWatcher;
self.prefs.removeObserver("", self);
},
observe: function (subject, topic, data)
{
var self = AntPrefs.prefWatcher;
if (topic != "nsPref:changed") {
AntLib.toLog("not nsPref:changed topic:" + topic);
return;
}
AntLib.toLog("prefWatcher: changed" +
" topic:" + topic +
" subject: " + subject +
" data: " + data);
if (data == 'rankmode')
{
var mode = AntPrefs.isRankMode;
var add = AntRankService.addButtonId;
var remove = AntRankService.removeButtonId;
var labels = AntBar.rankLabels;
if (mode)
{
AntLib.toLog('add buttons');
add(labels['statusbar-button'], labels['statusbar-separator']);
add(labels['toolbar-button']);
AntRankService.updateVisible();
}
else
{
AntLib.toLog('remove buttons');
remove(labels['statusbar-button'], labels['statusbar-separator']);
remove(labels['toolbar-button']);
}
}
AntFlvUi.updateMenuDownload(null);
AntPrefs.manageDisplayMode();
}
},
/**
* The old version of the toolbar (<= 1.3) added the "Ant Toolbar x.x"
* direcly in the general.useragent.extra.firefox String
* This function clean it up
*/
removeJunkAgent: function ()
{
var branch = AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("general.useragent.extra.");
var ff_agent = branch.getCharPref("firefox");
var trim = function (s) {
return s.replace(/^\s+/, '').replace(/\s+$/, '');
}
var m;
var modif = false;
while (m = ff_agent.match(/(.*)Ant\.com Toolbar [0-9]\.[0-9](.*)/))
{
var prefix = trim(m[1]);
var sufix = trim(m[2]);
ff_agent = trim((sufix.length == 0) ? (prefix) : (prefix + ' ' + sufix));
modif = true;
}
if (modif)
branch.setCharPref('firefox', ff_agent);
},
/**
* If the user Agent does not contain Ant.com we add it
*/
setAntUserAgent: function ()
{
var self = AntPrefs;
// Let's not pullute the firefox pref, instead use our own
// See https://developer.mozilla.org/En/User_Agent_Strings_Reference#Implementation_notes_for_applications.2c_vendors.2c_and_extensions
var branch = AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("general.useragent.extra.");
branch.setCharPref("anttoolbar", "Ant.com Toolbar " + self.version);
},
/**
* We remove the useragent at uninstall time
*/
removeAntUserAgent: function ()
{
var branch = AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("general.useragent.extra.");
branch.clearUserPref("anttoolbar");
},
/**
* Remove the addon preferences at uninstall
*/
cleanEnvironement: function ()
{
var branch = AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.deleteBranch("extensions.anttoolbar.");
},
/**
* Return the domain from an url
*/
getDomain: function (url)
{
url += "";
if (!url)
return false;
var match = url.match(/^https?:\/\/(?:[\w]+\.)*([\w-]+\.[\w-]+)/);
if (!match || match.length < 2)
return false;
return match[1];
},
/**
* return the Ant pref branch
*/
getAntBranch: function ()
{
return AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("extensions.anttoolbar.");
},
/**
* make Flash trust our flv player
*/
flashTrusting: function ()
{
var self = AntPrefs;
var home = AntLib.CCSV("@mozilla.org/file/directory_service;1", "nsIProperties").get("Home", AntLib.CI("nsIFile"));
var foStream = AntLib.CCIN("@mozilla.org/network/file-output-stream;1", "nsIFileOutputStream");
var data = "chrome://antbar/content/player/\n";
var os = AntLib.getOsName();
switch (os)
{
case "Linux":
home = self.mkchdir(home, ".macromedia");
if (!home)
return ;
home = self.mkchdir(home, "Flash_Player");
break;
case "WINNT":
if (!self.mkchdir(home, "Application Data"))
return;
if (!self.mkchdir(home, "Macromedia"))
return ;
if (!self.mkchdir(home, "Flash Player"))
return ;
break;
case "Darwin":
if (!self.mkchdir(home, "Library"))
return ;
if (!self.mkchdir(home, "Preferences"))
return ;
if (!self.mkchdir(home, "Macromedia"))
return ;
if (!self.mkchdir(home, "Flash Player"))
return ;
break;
default:
AntLib.toLog("os Not found in AntPrefs");
}
if (!home)
return ;
home = self.mkchdir(home, "#Security");
if (!home)
return ;
home = self.mkchdir(home, "FlashPlayerTrust");
home.append("antbar.cfg");
if (home.exists())
return ;
home.create(AntLib.CI("nsIFile").NORMAL_FILE_TYPE, 0777);
foStream.init(home, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate
foStream.write(data, data.length);
foStream.close();
},
/**
* Create the directory if it does not exist and change dir
*/
mkchdir: function (dir, to)
{
dir.append(to);
if (!dir.exists())
{
try
{
dir.create(AntLib.CI("nsIFile").DIRECTORY_TYPE, 0777);
}
catch (e)
{
AntLib.toLog("AntPrefs.mkchdir ERROR = " + e);
return false;
}
}
if (!dir.isDirectory())
{
return false;
}
return dir;
},
/**
* Geters/Seters for extension preferences
*/
getDisplayMode: function()
{
var self = AntPrefs;
var value = self.getAntBranch().getCharPref('mode');
if (value != 'statusbar')
return 'toolbar';
return value;
},
setDisplayMode: function(value)
{
var self = AntPrefs;
self.getAntBranch().setCharPref("mode", value);
},
get flvDir()
{
var self = AntPrefs;
return self.getAntBranch().getCharPref("flvdir");
},
set flvDir(value)
{
var self = AntPrefs;
self.getAntBranch().setCharPref("flvdir", value);
},
get flvToPlay()
{
var self = AntPrefs;
return self.getAntBranch().getCharPref("flvToPlay");
},
set flvToPlay(value)
{
var self = AntPrefs;
self.getAntBranch().setCharPref("flvToPlay", value);
},
get isRankMode()
{
var self = AntPrefs;
return self.getAntBranch().getBoolPref("rankmode");
},
set isRankMode(value)
{
var self = AntPrefs;
self.getAntBranch().setBoolPref("rankmode", value);
},
get version()
{
var self = AntPrefs;
return self.getAntBranch().getCharPref("version");
},
set version(value)
{
var self = AntPrefs;
self.getAntBranch().setCharPref("version", value);
},
get isNotification()
{
var self = AntPrefs;
return self.getAntBranch().getBoolPref("notification");
},
set isNotification(value)
{
var self = AntPrefs;
self.getAntBranch().setBoolPref("notification", value);
},
get canSendStats()
{
var self = AntPrefs;
return self.getAntBranch().getBoolPref("videorepports");
}
};